package com.logic.landseaserver.ws;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.exception.BusinessException;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.common.LandseaErrorMessageEnum;
import com.logic.landseaserver.common.exception.LandseaBusinessException;
import com.logic.landseaserver.service.IOrderService;
import com.logic.landseaserver.ws.dto.OrderDTO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;


/**
 * Created by Drake on 2017/4/17.
 */
@Path("/order")
@Api(value="order")
public class OrderRest {

	@Autowired
	private IOrderService iOrderServiceImpl;

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
    @GET
    @Path("/{orderId}")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Get order detail by project id", position = 4)
    public Response retrieveProjectById(
            @ApiParam(value = "param", required = true) @PathParam("orderId") Integer orderId) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(iOrderServiceImpl.get(orderId));
        } catch (Exception e) {
            logger.error("retrieveProjectById error.", e);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

	//增加
	@POST
	@Path("/createOrder")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "Create a new order",position = 1)
	public Response createOrder(@ApiParam(value = "param", required = true) OrderDTO orderDTO) {
		CommonResultMap map = new CommonResultMap();
        try {
        	int id = iOrderServiceImpl.insertOrder(orderDTO);
        	if(id == -1) {
        	    throw new LandseaBusinessException(LandseaErrorMessageEnum.ORDER_DUPLICATED);
            }
            orderDTO.setId(id);
            map.setData(orderDTO);

            //预约成功通知单
            iOrderServiceImpl.sendMessage(orderDTO); //Added by Daniel on 20170620

            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("createOrder error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("createOrder error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}
	
	//查询所有订单
	@POST
	@Path("/list")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "list orders",position = 2)
	public Response list(@ApiParam(value = "param", required = true) QueryUtil queryUtil){
        CommonResultMap map = new CommonResultMap();
        List<OrderDTO> orderDTOList = null;
        try {
            queryUtil.getPagingTool().calculateStartIndex();
            orderDTOList = (List<OrderDTO>)iOrderServiceImpl.query(queryUtil);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setData(orderDTOList);
            map.setResultTotalCount(totalCount);
            map.setResult(CommonResult.SUCCESS);

        } catch (Exception e) {
            logger.error("list error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();		
	}
	//删除
    @DELETE
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "logic delete order by set deleted", position = 3)
    public Response logicDelete(@ApiParam(value = "param", required = true) @QueryParam("orderId") Integer id) {
        CommonResultMap map = new CommonResultMap();
        try {
        	iOrderServiceImpl.deleteOrder(id);
            map.setResult(CommonResult.SUCCESS,"delete order");
        } catch (Exception e) {
            logger.error("logicDelete error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    //编辑
    @PUT
	@Path("/editOrder")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "Edit order ")
    public Response updateOrder(@ApiParam(value = "param", required = true) OrderDTO orderDTO) {
        CommonResultMap map = new CommonResultMap();
        try {
        	iOrderServiceImpl.editOrder(orderDTO);
            map.setResult(CommonResult.SUCCESS,"edit order");
        } catch (Exception e) {
            logger.error("updateOrder error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
}
